package com.bell.atensettings.wifi;

import static android.net.wifi.WifiConfiguration.INVALID_NETWORK_ID;
import com.bell.atensettings.R;
import com.bell.atensettings.bean.AccessPoint;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;

public class WifiDialog extends AlertDialog implements WifiConfigUiBase
{
    public static final int BUTTON_SUBMIT = DialogInterface.BUTTON_POSITIVE;
    public static final int BUTTON_FORGET = DialogInterface.BUTTON_NEUTRAL;

    private final boolean mEdit;
    private final DialogInterface.OnClickListener mListener;
    private final AccessPoint mAccessPoint;

    private View mView;
    private WifiConfigController mController;

    public WifiDialog(Context context, DialogInterface.OnClickListener listener, AccessPoint accessPoint, boolean edit) {
        super(context, R.style.dialog);
        mEdit = edit;
        mListener = listener;
        mAccessPoint = accessPoint;
    }

    @Override
    public WifiConfigController getController()
    {
        return mController;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        mView = getLayoutInflater().inflate(R.layout.wifi_dialog, null);
        setView(mView);
        setInverseBackgroundForced(true);
        mController = new WifiConfigController(this, mView, mAccessPoint, mEdit);
        super.onCreate(savedInstanceState);
        if (mAccessPoint != null) {
            if (mAccessPoint.getNetworkId() == INVALID_NETWORK_ID) {
                getSubmitButton().setEnabled(false);
            }
        }
        LayoutParams layoutparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

        if (getSubmitButton() != null) {
            getSubmitButton().setLayoutParams(layoutparams);
            getSubmitButton().setBackgroundResource(R.drawable.confirm_btnselector);
        }
        if (getCancelButton() != null) {
            getCancelButton().setLayoutParams(layoutparams);
            getCancelButton().setBackgroundResource(R.drawable.confirm_btnselector);
        }
        if (getForgetButton() != null) {
            getForgetButton().setLayoutParams(layoutparams);
            getForgetButton().setBackgroundResource(R.drawable.confirm_btnselector);
        }
    }

    @Override
    public boolean isEdit()
    {
        return mEdit;
    }

    @Override
    public Button getSubmitButton()
    {
        return getButton(BUTTON_SUBMIT);
    }

    @Override
    public Button getForgetButton()
    {
        return getButton(BUTTON_FORGET);
    }

    @Override
    public Button getCancelButton()
    {
        return getButton(BUTTON_NEGATIVE);
    }

    @Override
    public void setSubmitButton(CharSequence text)
    {
        setButton(BUTTON_SUBMIT, text, mListener);
    }

    @Override
    public void setForgetButton(CharSequence text)
    {
        setButton(BUTTON_FORGET, text, mListener);
    }

    @Override
    public void setCancelButton(CharSequence text)
    {
        setButton(BUTTON_NEGATIVE, text, mListener);
    }
}
